home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Sound / PreludeAMP / src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-16  |  6.2 KB  |  299 lines

  1.  
  2. #define AUDIO
  3. #define BUFSIZE  18432 * 8
  4.  
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #include <proto/exec.h>
  11. #include <exec/types.h>
  12. #include <exec/libraries.h>
  13. #include <exec/lists.h>
  14. #include <exec/memory.h>
  15. #include <libraries/prelude.h>
  16. #include <clib/prelude_protos.h>
  17. #include <ppcpragmas/prelude_pragmas.h>
  18. #include <proto/dos.h>
  19.  
  20. #include "amp.h"
  21. #include "audio.h"
  22. #include "getbits.h"
  23. #include "huffman.h"
  24. #include "layer2.h"
  25. #include "layer3.h"
  26. #include "transform.h"
  27. #include "misc2.h"
  28. #include "dump.h"
  29.  
  30. extern struct ExecBase *SysBase;
  31. struct PreludeBase *PreludeBase=NULL;
  32.  
  33. BYTE    signal;
  34. ULONG    Signals;
  35.  
  36. UBYTE    *PlayBuffer[2];
  37. ULONG    Buffer;
  38. ULONG BufferFill;
  39. UBYTE *BufferPointer;
  40.  
  41. ULONG PlayCnt;
  42. ULONG PlayMode;
  43. ULONG PlayFreq;
  44. BOOL snd_eof;
  45.  
  46.  
  47. int main(int argc, char **argv) {
  48.     struct Task *ThisTask;
  49.     struct PrlCtrl *prl;
  50.     BYTE OldPri;
  51.  
  52.     ThisTask=FindTask(NULL);
  53.  
  54.     PreludeBase=(struct PreludeBase *)OpenLibrary("prelude.library", 2);
  55.     if(PreludeBase==NULL) {
  56.         fprintf(stderr, "\nError: Can't open prelude.library v2\n\n");
  57.         return(5);
  58.     }
  59.  
  60.     PlayBuffer[0]=PPCAllocVec(BUFSIZE, MEMF_CLEAR|MEMF_PUBLIC);
  61.     PlayBuffer[1]=PPCAllocVec(BUFSIZE, MEMF_CLEAR|MEMF_PUBLIC);
  62.  
  63.     if(PlayBuffer[0]==NULL || PlayBuffer[1]==NULL) {
  64.         if(PlayBuffer[0]) PPCFreeVec(PlayBuffer[0]);
  65.         fprintf(stderr, "\nError: Out of memory for playback buffers\n\n");
  66.         return(5);
  67.     }
  68.  
  69.     prl=PreludeBase->PrlCtrl;
  70.     prl->PL_SigTask=ThisTask;
  71.     signal=AllocSignal(-1);
  72.     prl->PL_SigMask=1L<<signal;
  73.  
  74.     A_DUMP_BINARY=FALSE;
  75.     A_QUIET=FALSE;
  76.     A_FORMAT_WAVE=FALSE;
  77.     A_SHOW_CNT=FALSE;
  78.     A_SET_VOLUME=-1;
  79.     A_SHOW_TIME=1;
  80.     A_AUDIO_PLAY=FALSE;
  81.     A_WRITE_TO_FILE=TRUE;
  82.     A_MSG_STDOUT=FALSE;
  83.     A_DOWNMIX=FALSE;
  84.  
  85.  
  86.     initialise_decoder();
  87.  
  88.     if(argc != 2) {
  89.         fprintf(stderr, "\nUsage PreludeAMP [infilename]\n\n");
  90.         return(5);
  91.     }
  92.  
  93.     Buffer=0;
  94.     BufferPointer=PlayBuffer[0];
  95.     BufferFill=0;
  96.     PlayCnt=0;
  97.  
  98.     fprintf(stderr, "\n\
  99. -------------------------------------------\n\
  100. PreludeAMP 0.4 - (C) 1998, by Thomas Wenzel\n\
  101. based on amp (C) Tomislav Uzelac  1996,1997\n\
  102. -------------------------------------------\n\
  103. ");
  104.  
  105.     OldPri=SetTaskPri(ThisTask, 5);
  106.     play(argv[1], 0);
  107.     SetTaskPri(ThisTask, OldPri);
  108.  
  109.     PrlStop(0);
  110.     KillPrlPlayList();
  111.  
  112.     PPCFreeVec(PlayBuffer[0]);
  113.     PPCFreeVec(PlayBuffer[1]);
  114.  
  115.     if(signal != -1) FreeSignal(signal);
  116.  
  117.     CloseLibrary((struct Library *) PreludeBase);
  118.     return(0);
  119. }
  120.  
  121.  
  122.  
  123. /* call this once at the beginning */
  124. void initialise_decoder(void) {
  125.     premultiply();
  126.     imdct_init();
  127.     calculate_t43();
  128. }
  129.  
  130. /* call this before each file is played */
  131. void initialise_globals(void) {
  132.     append=data=nch=0; 
  133.     f_bdirty=TRUE;
  134.     bclean_bytes=0;
  135.  
  136.     memset(s,0,sizeof s);
  137.     memset(res,0,sizeof res);
  138. }
  139.  
  140.  
  141. void play(char *inFileStr, char *outFileStr) {
  142.     if (strcmp(inFileStr,"-")==0) {
  143.         in_file=stdin;
  144.     }
  145.     else {
  146.         if ((in_file=fopen(inFileStr,"r"))==NULL) {
  147.             fprintf(stderr, "Could not open file: %s\n",inFileStr);
  148.             return;
  149.         }
  150.     }
  151.     if (outFileStr) {
  152.         if (strcmp(outFileStr,"-")==0)
  153.             out_file=stdout;
  154.     else
  155.         if ((out_file=fopen(outFileStr,"w"))==NULL) {
  156.             fprintf(stderr, "Could not write to file: %s\n",outFileStr);
  157.             return;
  158.         }
  159.     }
  160.  
  161.     decodeMPEG();
  162.     
  163.     fclose(in_file);
  164.     if (!A_AUDIO_PLAY) fclose(out_file);
  165.     fprintf(stderr, "\n");
  166. }
  167.  
  168. int decodeMPEG(void) {
  169.     struct AUDIO_HEADER header;
  170.     int cnt=0,g;
  171.  
  172.     initialise_globals();
  173.  
  174.     if ((g=gethdr(&header))!=0) {
  175.         report_header_error(g);
  176.         return -1;
  177.     }
  178.  
  179.     if (header.protection_bit==0) getcrc();
  180.  
  181.     show_header(&header);
  182.  
  183.     if (header.layer==1) {
  184.         if (layer3_frame(&header,cnt)) {
  185.             fprintf(stderr, " error. blip.\n");
  186.             return -1;
  187.         }
  188.     } else if (header.layer==2)
  189.         if (layer2_frame(&header,cnt)) {
  190.             fprintf(stderr, " error. blip.\n");
  191.             return -1;
  192.         }
  193.  
  194.  
  195.     if (nch==2) PlayMode = PRL_FMT | PRL_FMTX | PRL_Stereo;
  196.     else        PlayMode = PRL_FMT | PRL_FMTX;
  197.     PlayFreq=t_sampling_frequency[header.ID][header.sampling_frequency];
  198.  
  199.     /*
  200.      * decoder loop **********************************
  201.      */
  202.     snd_eof=FALSE;
  203.     cnt=0;
  204.     while (!snd_eof) {
  205.         while (!snd_eof) {
  206.             if ((g=gethdr(&header))!=0) {
  207.             report_header_error(g);
  208.                 snd_eof=TRUE;
  209.                 break;
  210.       }
  211.  
  212.             if (header.protection_bit==0) getcrc();
  213.  
  214.             statusDisplay(&header,cnt);    
  215.  
  216.             if (header.layer==1) {
  217.                 if (layer3_frame(&header,cnt)) {
  218.                     fprintf(stderr, " error. blip.\n");
  219.                     return -1;
  220.                 }
  221.             } else if (header.layer==2)
  222.                 if (layer2_frame(&header,cnt)) {
  223.                     fprintf(stderr, " error. blip.\n");
  224.                     return -1;
  225.                 }
  226.             cnt++;
  227.         }
  228.     }
  229.     return 0;
  230. }
  231.  
  232. void report_header_error(int err) {
  233.     switch (err) {
  234.         case GETHDR_ERR: fprintf(stderr, "error reading mpeg bitstream. exiting.\n");
  235.                          break;
  236.         case GETHDR_NS:  fprintf(stderr, "this is a file in MPEG 2.5 format, which is not defined\n");
  237.                          fprintf(stderr, "by ISO/MPEG. It is \"a special Fraunhofer format\".\n");
  238.                          fprintf(stderr, "amp does not support this format. sorry.\n");
  239.                          break;
  240.         case GETHDR_FL1: fprintf(stderr, "ISO/MPEG layer 1 is not supported by amp (yet).\n");
  241.                          break;
  242.         case GETHDR_FF:  fprintf(stderr, "free format bitstreams are not supported. sorry.\n");
  243.                          break;    
  244.         case GETHDR_SYN: fprintf(stderr, "oops, we're out of sync.\n");
  245.                      break;
  246.         case GETHDR_EOF: break;
  247.     }    
  248. }
  249.  
  250.  
  251. void statusDisplay(struct AUDIO_HEADER *header, int frameNo) {
  252.     int minutes,seconds;
  253.  
  254.     if ((A_SHOW_CNT || A_SHOW_TIME) && !(frameNo%10))
  255.         fprintf(stderr, "\r");
  256.     if (A_SHOW_CNT && !(frameNo%10) ) {
  257.         fprintf(stderr, "Frame { %d } ",frameNo);
  258.     }
  259.     if (A_SHOW_TIME && !(frameNo%10)) {
  260.         seconds=frameNo*1152/t_sampling_frequency[header->ID][header->sampling_frequency];
  261.         minutes=seconds/60;
  262.         seconds=seconds % 60;
  263.         fprintf(stderr, "Time [%d:%02d]", minutes, seconds);
  264.     }
  265.     if (A_SHOW_CNT || A_SHOW_TIME)
  266.         fflush(stderr);
  267. }
  268.  
  269. BOOL WayBehind=FALSE;
  270. void printout(void) {
  271.     int len;
  272.     int j;
  273.  
  274.     if (nch==2) j=32 * 18 * 2;
  275.     else        j=32 * 18;
  276.  
  277.     len=sizeof(short)*j;
  278.  
  279.     memcpy(BufferPointer, sample_buffer, len);
  280.     BufferPointer += len;
  281.     BufferFill    += len;
  282.  
  283.     if(BufferFill >= BUFSIZE) {
  284.         PrlPlay(PlayBuffer[Buffer], BUFSIZE, PlayMode, PlayFreq);
  285.         Buffer=1-Buffer;
  286.  
  287.         BufferPointer=PlayBuffer[Buffer];
  288.         BufferFill=0;
  289.  
  290.         PlayCnt++;
  291.         if(PlayCnt >1) Signals=Wait(1L<<signal | SIGBREAKF_CTRL_C);
  292.  
  293.         if(Signals & SIGBREAKF_CTRL_C) snd_eof=TRUE;
  294.     }
  295. }
  296.  
  297. void die(char *str, ...) {
  298. }
  299.